
#include "pch.h"
#include "CubeMesh.h"
#include "../DirectX/DirectXHelper.h"
#include "../Rendering/ConstantBuffers.h"

using namespace Microsoft::WRL;
using namespace DirectX;

CubeMesh::CubeMesh(_In_ ID3D11Device *device, ID3D11InputLayout* inputLayout) :
    MeshObject(inputLayout)
{
    D3D11_BUFFER_DESC bd = {0};
    D3D11_SUBRESOURCE_DATA initData = {0};
    PNTVertex target_vertices[] =
    {
		// 0,1,2  1,3,2
		{XMFLOAT3(-0.5f, 0.5f, 0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // +Y (top face)
        {XMFLOAT3( 0.5f, 0.5f, 0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
        {XMFLOAT3( -0.5f, 0.5f, -0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },
        {XMFLOAT3(0.5f, 0.5f,  -0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },

		//0,1,2  1,3,2
        {XMFLOAT3(-0.5f, -0.5f,  -0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // -Y (bottom face)
        {XMFLOAT3( 0.5f, -0.5f,  -0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
        {XMFLOAT3 (-0.5f, -0.5f, 0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },
        {XMFLOAT3(0.5f, -0.5f, 0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },

		// 0,1,2  1,3,2
        {XMFLOAT3(-0.5f,  0.5f,  0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // +X (right face)
        {XMFLOAT3(-0.5f,  0.5f, -0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
        {XMFLOAT3(-0.5f, -0.5f, 0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },
        {XMFLOAT3(-0.5f, -0.5f,  -0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },

		// 0,1,2  1,3,2
        {XMFLOAT3(0.5f,  0.5f, -0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // -X (left face)
        {XMFLOAT3(0.5f,  0.5f,  0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
        {XMFLOAT3(0.5f, -0.5f,  -0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },
        {XMFLOAT3(0.5f, -0.5f, 0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },


		// 0,1,2  1,3,2
        {XMFLOAT3(0.5f,  0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(0.0f, 0.0f) }, // +Z (front face)
        {XMFLOAT3( -0.5f,  0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(1.0f, 0.0f) },
        {XMFLOAT3( 0.5f, -0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(0.0f, 1.0f) },
        {XMFLOAT3(-0.5f, -0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(1.0f, 1.0f) },

		// 0,1,2  1,3,2
        {XMFLOAT3( -0.5f,  0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(0.0f, 0.0f) }, // -Z (back face)
        {XMFLOAT3(0.5f,  0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(1.0f, 0.0f) },
        {XMFLOAT3(-0.5f, -0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(0.0f, 1.0f) },
        {XMFLOAT3(0.5f, -0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(1.0f, 1.0f) }

	
  //	  {XMFLOAT3(-0.5f, 0.5f, -0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // +Y (top face)
  //      {XMFLOAT3( 0.5f, 0.5f, -0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
  //      {XMFLOAT3( 0.5f, 0.5f,  0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },
  //      {XMFLOAT3(-0.5f, 0.5f,  0.5f),XMFLOAT3(0.0f, 1.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },

  //      {XMFLOAT3(-0.5f, -0.5f,  0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // -Y (bottom face)
  //      {XMFLOAT3( 0.5f, -0.5f,  0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
  //      {XMFLOAT3 (0.5f, -0.5f, -0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },
  //      {XMFLOAT3(-0.5f, -0.5f, -0.5f),XMFLOAT3(0.0f, -1.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },

  //      {XMFLOAT3(0.5f,  0.5f,  0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // +X (right face)
  //      {XMFLOAT3(0.5f,  0.5f, -0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
  //      {XMFLOAT3(0.5f, -0.5f, -0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },
  //      {XMFLOAT3(0.5f, -0.5f,  0.5f),XMFLOAT3(-1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },

  //      {XMFLOAT3(-0.5f,  0.5f, -0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 0.0f) }, // -X (left face)
  //      {XMFLOAT3(-0.5f,  0.5f,  0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 0.0f) },
  //      {XMFLOAT3(-0.5f, -0.5f,  0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(1.0f, 1.0f) },
  //      {XMFLOAT3(-0.5f, -0.5f, -0.5f),XMFLOAT3(1.0f, 0.0f, 0.0f),XMFLOAT2(0.0f, 1.0f) },

  //      {XMFLOAT3(-0.5f,  0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(0.0f, 0.0f) }, // +Z (front face)
  //      {XMFLOAT3( 0.5f,  0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(1.0f, 0.0f) },
  //      {XMFLOAT3( 0.5f, -0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(1.0f, 1.0f) },
  //      {XMFLOAT3(-0.5f, -0.5f, 0.5f),XMFLOAT3(0.0f, 0.0f, 1.0f),XMFLOAT2(0.0f, 1.0f) },

  //      {XMFLOAT3( 0.5f,  0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(0.0f, 0.0f) }, // -Z (back face)
  //      {XMFLOAT3(-0.5f,  0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(1.0f, 0.0f) },
  //      {XMFLOAT3(-0.5f, -0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(1.0f, 1.0f) },
  //      {XMFLOAT3( 0.5f, -0.5f, -0.5f),XMFLOAT3(0.0f, 0.0f, -1.0f),XMFLOAT2(0.0f, 1.0f) }
    };

	    
	WORD target_indices[] =
    {
        0, 1, 2,
        1, 3, 2,

        4, 5, 6,
        5, 7, 6,

        8, 9, 10,
        9, 11, 10,

        12, 13, 14,
        13, 15, 14,

        16, 17, 18,
        17, 19, 18,

        20, 21, 22,
        21, 23, 22
    };

    //WORD target_indices[] =
    //{
    //    0, 1, 2,
    //    0, 2, 3,

    //    4, 5, 6,
    //    4, 6, 7,

    //    8, 9, 10,
    //    8, 10, 11,

    //    12, 13, 14,
    //    12, 14, 15,

    //    16, 17, 18,
    //    16, 18, 19,

    //    20, 21, 22,
    //    20, 22, 23
    //};

    m_stride = sizeof(PNTVertex);
    
    m_vertexCount = ARRAYSIZE(target_vertices);
    m_indexCount = ARRAYSIZE(target_indices);

    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(target_vertices);
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = 0;
    initData.pSysMem = target_vertices;
    DX::ThrowIfFailed(
        device->CreateBuffer(&bd, &initData, &m_vertexBuffer)
        );

    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(target_indices);
    bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    bd.CPUAccessFlags = 0;
    initData.pSysMem = target_indices;
    DX::ThrowIfFailed(
        device->CreateBuffer(&bd, &initData, &m_indexBuffer)
        );
}